package yygh.hosp.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import yygh.hospital.model.hosp.HospitalSet;
import yygh.hospital.vo.hosp.HospitalSetQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yygh.common.result.Result;
import yygh.common.util.MD5;
import yygh.hosp.Service.HospitalSetService;
import java.util.List;
import java.util.Random;


@Api(tags = "医院接口设置")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    //注入Service
    @Autowired
   private HospitalSetService hospitalSetService;

    //医院设置相关接口1-查询所有的医院设置信息
    @ApiOperation("查找所有医院信息")
    @GetMapping("/findAll")
    public Result findAll(){
        List<HospitalSet> list = hospitalSetService.list();
        return  Result.ok(list);
    }

    //医院设置相关接口2：根据id编号逻辑删除指定医院设置
    @ApiOperation("删除指定编号的医院设置")
    @DeleteMapping("{id}")
    public  Result removeHospitalSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag){
            //删除成功
            return  Result.ok();
        }else {
            return Result.fail();
        }
    }

    //医院接口3：条件查询+分页查询医院信息
    //其中查询条件为根据医院名称和医院编号进行查询，将其封装在hospitalSetQuery实体类中
    @ApiOperation("分页条件查询")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public  Result pageHospitalSet(@PathVariable Long current,
                                   @PathVariable Long limit,
                                   @RequestBody(required = false)HospitalSetQuery hospitalSetQuery){
     //根据传入的参数current和limit创建一个page实例化对象
        Page<HospitalSet> page = new Page<>(current, limit);
    //条件查询开启，根据请求体json对象中的医院名称和医院编号进行条件查询
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        String hosname = hospitalSetQuery.getHosname();
        if (!StringUtils.isEmpty(hosname)){
            //如果请求体中的hosname存在，才能对其进行条件查询
            queryWrapper.like("hosname",hosname);
        }
        String hoscode = hospitalSetQuery.getHoscode();
        if (!StringUtils.isEmpty(hoscode)){
            //如果请求体中的hoscode存在，才能对其进行条件查询
            queryWrapper.ge("hoscode",hoscode);
        }
        //调用mubatis-plus分页条件查询方法
        IPage<HospitalSet> pageHospSet= hospitalSetService.page(page, queryWrapper);
        //返回统一的结果
        return  Result.ok(pageHospSet);
    }


    //医院接口4：添加医院信息设置
    @ApiOperation("添加医院信息")
    @PostMapping("saveHospitalSet")
    public  Result save(@RequestBody HospitalSet hospitalSet){
        //传入一个hospitalSet，进行添加
        //在传入医院信息之前，首先要设置要添加的医院信息的状态status为1（0表示不可用）
        hospitalSet.setStatus(1);
        //在传入医院信息之前，首先要设置要添加的医院信息的签名密钥
        //根据随机数和当前时间戳生成一个签名密钥
        Random random=new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        //添加一个医院设置
        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return  Result.ok();
        }else {
            return  Result.fail();
        }
    }


    //医院接口5：根据id获取医院设置
    @ApiOperation("根据id获取医院设置")
    @GetMapping("getHospitalSet/{id}")
    public  Result getHospSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return  Result.ok(hospitalSet);
    }


    //医院设置相关接口5：修改医院设置
    @ApiOperation("根据id修改医院设置")
    @PostMapping("updateHospitalSet")
    //医院接口6：修改医院设置
    public  Result updateHospSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag){
            return  Result.ok();
        }else {
            return  Result.fail();
        }
    }



    //医院接口7:批量删除医院设置
    @ApiOperation("批量删除医院信息")
    @DeleteMapping("batchRemove")
    public  Result batchDeleteHospitalSet(@PathVariable List<Long> idList){
        boolean flag = hospitalSetService.removeByIds(idList);
        if (flag){
            return Result.ok();
        }else {
            return  Result.fail();
        }
    }


    //医院接口8：根据id对指定医院进行设定锁和解锁
    @ApiOperation("对指定医院进行设定锁和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public  Result lockHospitalSet(@PathVariable Long id,
                                   @PathVariable Integer status){
        //首先根据id查找医院信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置status值，设置状态
        hospitalSet.setStatus(status);
        //对医院信息进行更新
        hospitalSetService.updateById(hospitalSet);
        //返回统一结果
        return Result.ok();
    }

    //医院接口9：发送签名密钥
    @ApiOperation("发送签名密钥")
    @PutMapping("sendKey/{id}")
    public  Result sendkey(@PathVariable Long id){
        //根据id查询医院信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //得到签名密钥
        String signKey = hospitalSet.getSignKey();
        return  Result.ok(signKey);

    }
}
